package com.ruoyi.mentor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.mentor.bo.InsPaperAddBo;
import com.ruoyi.mentor.bo.InsPaperEditBo;
import com.ruoyi.mentor.bo.InsPaperQueryBo;
import com.ruoyi.mentor.domain.InsPaper;
import com.ruoyi.mentor.mapper.InsPaperMapper;
import com.ruoyi.mentor.service.IInsPaperService;
import com.ruoyi.mentor.vo.InsPaperVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 导师科研论文Service业务层处理
 *
 * @author LH
 * @date 2021-05-21
 */
@Service
public class InsPaperServiceImpl extends ServiceImpl<InsPaperMapper, InsPaper> implements IInsPaperService {

    @Override
    public InsPaperVo queryById(Long id){
        return getVoById(id, InsPaperVo.class);
    }

    @Override
    public TableDataInfo<InsPaperVo> queryPageList(InsPaperQueryBo bo) {
        PagePlus<InsPaper, InsPaperVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), InsPaperVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<InsPaperVo> queryList(InsPaperQueryBo bo) {
        return listVo(buildQueryWrapper(bo), InsPaperVo.class);
    }

    private LambdaQueryWrapper<InsPaper> buildQueryWrapper(InsPaperQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InsPaper> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getInsId()!=null, InsPaper::getInsId, bo.getInsId());
        lqw.eq(StrUtil.isNotBlank(bo.getDetail()), InsPaper::getDetail, bo.getDetail());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(InsPaperAddBo bo) {
        InsPaper add = BeanUtil.toBean(bo, InsPaper.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(InsPaperEditBo bo) {
        InsPaper update = BeanUtil.toBean(bo, InsPaper.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InsPaper entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
